/*
* BendController.java
* Created on  2015/7/27 15:16
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/7/27       longshihui    初始版本
*
*/

package com.infitecs.eyas.tools;

import com.infitecs.eyas.expression.service.ToolExpressionService;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.tool.Bend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/tools/bend")
public class BendController {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private ToolExpressionService toolExpressionService;

    @RequestMapping("")
    public String getPage(){
        sessionService.setEnvironment('T');
        //toolExpressionService.getParamMap().put("isChecked",0.0);
        return "tools/bend";
    }


    @RequestMapping(value = "/updateParam",method = RequestMethod.POST)
    @ResponseBody
    public void updateParam(Bend bend) {
        toolExpressionService.updateToolsParam(bend);
    }

    @RequestMapping(value = "/calE15",method = RequestMethod.GET)
    @ResponseBody
    public Double calE15() {
        return toolExpressionService.calculate("TE15");
    }

    @RequestMapping(value = "/clean",method = RequestMethod.POST)
    @ResponseBody
    public void clean() {
        toolExpressionService.clearMap();
        toolExpressionService.clearResultMap();
    }

    @RequestMapping(value = "/cal",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Double> cal() {
        Map<String,Double> map = new HashMap<>();
        toolExpressionService.calculate("TE14");
        toolExpressionService.calculate("TE16");
        map.put("e19", toolExpressionService.calculate("TE19"));
        map.put("e21", toolExpressionService.calculate("TE21"));
        calE19AndE20(map);
        return map;
    }

    private void calE19AndE20(Map<String, Double> map) {
        Map<String, Double> paramMap = toolExpressionService.getParamMap();
        Double E17 = toolExpressionService.calculate("TE17");
        Double E32 = paramMap.get("TE32");
        Double isChecked = paramMap.get("TisChecked");
        Double temp1 = E32 / E17;
        Double temp2 = E32 / E17;
        if (temp1 > 0.5) {
            toolExpressionService.calculate("TE34");
            toolExpressionService.calculate("TE35");
            toolExpressionService.calculate("TE36");
            toolExpressionService.calculate("TE37");
            toolExpressionService.calculate("TE38");
            toolExpressionService.calculate("TE39");
            map.put("e35",toolExpressionService.calculate("TE40"));
        } else if (temp1 <= 0.5 && isChecked == null && temp1 !=0) {
            toolExpressionService.calculate("TE43");
            toolExpressionService.calculate("TE44");
            toolExpressionService.calculate("TE45");
            toolExpressionService.calculate("TE46");
            toolExpressionService.calculate("TE47");
            toolExpressionService.calculate("TE48");
            map.put("e35",toolExpressionService.calculate("TE49"));
        } else if (temp1 == 0 && isChecked == null) {
            toolExpressionService.calculate("TE52");
            toolExpressionService.calculate("TE53");
            toolExpressionService.calculate("TE54");
            toolExpressionService.calculate("TE55");
            toolExpressionService.calculate("TE56");
            toolExpressionService.calculate("TE57");
            map.put("e35",toolExpressionService.calculate("TE58"));
        } else if (temp1 == 0 && isChecked == 1) {
            toolExpressionService.calculate("TE55");
            toolExpressionService.calculate("TE56");
            toolExpressionService.calculate("TE71");
            toolExpressionService.calculate("TE72");
            toolExpressionService.calculate("TE73");
            toolExpressionService.calculate("TE74");
            map.put("e35",toolExpressionService.calculate("TE75"));
        }else if (temp1 <= 0.5 && isChecked == 1 && temp1 != 0) {
            toolExpressionService.calculate("TE22");
            toolExpressionService.calculate("TE23");
            toolExpressionService.calculate("TE34");
            toolExpressionService.calculate("TE36");
            toolExpressionService.calculate("TE62");
            toolExpressionService.calculate("TE63");
            toolExpressionService.calculate("TE64");
            toolExpressionService.calculate("TE65");
            toolExpressionService.calculate("TE66");
            toolExpressionService.calculate("TE67");
            map.put("e35",toolExpressionService.calculate("TE68"));
        }
        if (temp2 > 0.5) {
            toolExpressionService.calculate("TE34");
            toolExpressionService.calculate("TE35");
            toolExpressionService.calculate("TE36");
            toolExpressionService.calculate("TE37");
            toolExpressionService.calculate("TE38");
            toolExpressionService.calculate("TE39");
            toolExpressionService.calculate("TE40");
            map.put("e36",toolExpressionService.calculate("TE41"));//最后有可能返回null
        } else if (temp2 <= 0.5 && isChecked == null && temp2 !=0) {
            map.put("e36",toolExpressionService.calculate("TE50"));
        } else if (temp2 == 0 && isChecked == null) {
            map.put("e36",toolExpressionService.calculate("TE59"));
        } else if (temp2 <= 0.5 && isChecked == 1 && temp2 != 0) {
            map.put("e36",toolExpressionService.calculate("TE69"));
        } else if (temp2 == 0 && isChecked == 1) {
            map.put("e36",toolExpressionService.calculate("TE76"));
        }
        toolExpressionService.getResult().put("p", map.get("e19"));
        toolExpressionService.getResult().put("z",map.get("e35"));
        toolExpressionService.getResult().put("lost",map.get("e35") * map.get("e19"));
    }

}
